package com.jiedeng.util;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.Region;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;

public class ExcelUtil {
    /**
     * 表格赋值
     *
     * @param wb
     * @param row    行
     * @param column 列
     * @param halign 左右对齐
     * @param valign 上下对齐
     */
    public static void createCell(Workbook wb, Row row, short column, String value, short halign, short valign) {
        Cell cell = row.getCell(column);
        if (cell == null) {
            cell = row.createCell(column);
        }
        CellStyle cellStyle = wb.createCellStyle();
        //自适应
        cellStyle.setWrapText(true);
        //水平对齐
        cellStyle.setAlignment(halign);
        //垂直对齐
        cellStyle.setVerticalAlignment(valign);
        // 设置边框样式
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }

    /**
     * 无边框
     *
     * @param wb
     * @param row
     * @param column
     * @param value
     */
    public static void createCellNoBorder(Workbook wb, Row row, short column, String value) {
        Cell cell = row.getCell(column);
        if (cell == null) {
            cell = row.createCell(column);
        }
        CellStyle cellStyle = wb.createCellStyle();
        //自适应
        cellStyle.setWrapText(true);
        //水平对齐
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        //垂直对齐
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(value);
    }

    public static void createCell(Workbook wb, Row row, short column, Object value, Font font) {
        Cell cell = row.getCell(column);
        if (cell == null) {
            cell = row.createCell(column);
        }
        CellStyle cellStyle = wb.createCellStyle();
        //自适应
        cellStyle.setWrapText(true);
        //水平对齐
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        //垂直对齐
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        cellStyle.setFont(font);
        // 设置边框样式
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cell.setCellStyle(cellStyle);
        if (StringUtils.isEmpty(value)) {
            value = "";
        }
        cell.setCellValue(value + "");
    }


    /**
     * 合并单元格边框设置
     *
     * @param sheet
     * @param region
     * @param cs
     */
    public static void setRegionStyle(XSSFSheet sheet, Region region, CellStyle cs) {
        for (int i = region.getRowFrom(); i <= region.getRowTo(); i++) {
            Row row = sheet.getRow(i);
            for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
                Cell cell = row.getCell((short) j);
                if (cell == null) {
                    cell = row.createCell((short) j);
                }
                cell.setCellStyle(cs);
            }
        }
    }


    /**
     * 下载模板
     *
     * @param uri      模板地址
     * @param filePath 目标路径
     * @return
     */

    public static boolean downloadSmallFile(String uri, String filePath) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(uri.toString()).build();

        try {
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                return false;
            }

            com.squareup.okhttp.ResponseBody body = response.body();
            long contentLength = body.contentLength();
            BufferedSource source = body.source();
            File file = new File(filePath);
            BufferedSink sink = Okio.buffer(Okio.sink(file));
            sink.writeAll(source);
            sink.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
